package com.cw.java8.builtInFunction;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Predicate;

/**
 * 谓词演示
 */
public class MyPredicate {

    public static void main(String[] args) {
        //boolean test(T t);
        Predicate<String> predicate = (String t) -> t.startsWith("bj");
        boolean flag = predicate.test("bjtedu");
        System.out.println(flag);

        // boolean test(T t);
        //全班成绩分数的list
        List<Double> list = Arrays.asList(58.5D, 34D, 68D, 98D, 65D, 78D, 89D, 58D, 96D);
        List<Double> resultList = filterList(list, (Double score) -> {
            if (score >= 60) {
                return true;
            } else {
                return false;
            }
        });

        for (Double score : resultList) {
            System.out.print(score + "  ");
        }
    }

    /**
     * 有一个需求，学生成绩及格的，即60分以上的过滤出来
     *
     * @param list
     * @param predicate
     * @param <T>
     * @return
     */
    public static <T> List<T> filterList(List<T> list, Predicate<T> predicate) {
        //结果List
        List<T> resultList = new LinkedList<>();

        //循环全班成绩的List
        for (T t : list) {
            //谓词判断，如果谓词返回true，true代表分数大于等于60分，就把分数放入结果List
            if (predicate.test(t)) {
                resultList.add(t);
            }
        }
        return resultList;
    }
}
